<?php
class PermissionsController extends AppController {
	public $name = 'Permissions';
	public $uses=array('Aco','Menu');	
	function index(){	
		
	}
	function lists(){	
		$this->set('data',$this->{$this->uses[0]}->findAll());
	}	
	function admin_buildMenu(){
		
		$this->Menu->query('truncate table menus');
		$menu=& $this->Menu;
		$menu->create(array('parent_id'=>null,'name'=>'root','link'=>null,'controller'=>null,'method'=>null,'action'=>1));
		$menu->save();
		
		$root_id=$menu->id;
		$menu->create(array('parent_id'=>$root_id,'name'=>'home','link'=>'homes/home','controller'=>null,'method'=>null,'action'=>1));
		$menu->save();
		
		
		$menu->create(array('parent_id'=>$root_id,'name'=>'user manager','link'=>null,'controller'=>null,'method'=>null,'action'=>1));
		$menu->save();
		$parent_id=$menu->id;		
		$menu->create(array('parent_id'=>$parent_id,'name'=>'users','link'=>'users','controller'=>'Users','method'=>'admin_index','action'=>1));
		$menu->save();
		$menu->create(array('parent_id'=>$parent_id,'name'=>'groups','link'=>'groups','controller'=>'Groups','method'=>'admin_index','action'=>1));
		$menu->save();
		$menu->create(array('parent_id'=>$parent_id,'name'=>'logout','link'=>BACKEND_URL.'users/logout','controller'=>'Users','method'=>'admin_logout','action'=>0));
		$menu->save();
		
		$menu->create(array('parent_id'=>$root_id,'name'=>'site manager','link'=>null,'controller'=>null,'method'=>null,'action'=>1));
		$menu->save();
		$parent_id=$menu->id;		
		$menu->create(array('parent_id'=>$parent_id,'name'=>'other page','link'=>'static_pages','controller'=>'StaticPages','method'=>'admin_index','action'=>1));
		$menu->save();
		$menu->create(array('parent_id'=>$parent_id,'name'=>'job manager','link'=>'jobs','controller'=>'Jobs','method'=>'admin_index','action'=>1));
		$menu->save();
		$menu->create(array('parent_id'=>$parent_id,'name'=>'news manager','link'=>'news','controller'=>'News','method'=>'admin_index','action'=>1));
		$menu->save();
		
		
		$menu->create(array('parent_id'=>$root_id,'name'=>'system config','link'=>null,'controller'=>null,'method'=>null,'action'=>1));
		$menu->save();
		$parent_id=$menu->id;		
		$menu->create(array('parent_id'=>$parent_id,'name'=>'configurations','link'=>'configs/config','controller'=>'Configs','method'=>'admin_index','action'=>1));
		$menu->save();		
		$menu->create(array('parent_id'=>$parent_id,'name'=>'config manager','link'=>'configs','controller'=>'Configs','method'=>'admin_config','action'=>1));
		$menu->save();
		
		$this->Menu->recover('parent');
	}
	
		
/**
 * Rebuild the Acl based on the current controllers in the application
 *
 * @return void
 */
	
	function admin_buildAco(){
		//group
		$group =& $this->Group;
		$group->id=1;
		//create root
		//$this->Aco->query('truncate table aros_acos');
		//$this->Aco->query('truncate table acos');
		
		$aco =& $this->Acl->Aco;
        $root = $aco->node('controllers');
        
        if (!$root) {
            $aco->create(array('parent_id' => null, 'model' => null, 'alias' => 'controllers'));
            $root = $aco->save();
            
            $root['Aco']['id'] = $aco->id; 
            $log[] = 'Created Aco node for controllers';
        } 
        else {
            $root = $root[0];
        }
		// Get Controllers and Controller's Methods
 		$path = APP."controllers";
 		$folder =& new Folder($path);
 		$controllers = $folder->read(); 		
 		$methods = array(); 		
 		 		
 		foreach($controllers[1] as $controller){
 			$className = str_replace('_controller.php','',$controller);
 			$className = Inflector::camelize($className);
 			if(!in_array($className,$this->Common->baseController())){
	 			
	 			// find / make controller node
	            $controllerNode = $aco->node('controllers/'.$className);
	            if (!$controllerNode) {
	                $aco->create(array('parent_id' => $root['Aco']['id'], 'model' => null, 'alias' => $className));
	                $controllerNode = $aco->save();
	                $controllerNode['Aco']['id'] = $aco->id;
	                $log[] = 'Created Aco node for '.$className;
	                  $this->Acl->allow($group,$className);
	            } else {
	                $controllerNode = $controllerNode[0];
	            }
	 			//$methods[$className][$className] = $className;
	 			App::import('Controller',$className);
	 			$classNames = $className."Controller";	 			
	 			$class =& new $classNames();	
	 			$_methods = get_class_methods($class); 			
	 			foreach($_methods as $method){
	 				if($method != "beforeFilter"){	 
	 					
	 					
	 					if(strpos($method,"_",0) !== 0 && strpos($method,"admin",0)===0 && !in_array($method,$this->Common->baseMethod()) && !in_array($className.'/'.$method,$this->Common->baseMethodController())){
	 						$methods[$className][$method] = $method;
	 						//get same permission
	 						$methodPath=$this->Common->samePermission('controllers/'.$className.'/'.$method);
	 						$methodNode = $aco->node($methodPath);
			                if (!$methodNode) {
			                    $aco->create(array('parent_id' => $controllerNode['Aco']['id'], 'model' => null, 'alias' => $method));
			                    $methodNode = $aco->save();
			                    $log[] = 'Created Aco node for '.$methodPath;
			                    //set permission for superadmin
			                    
			                  
			                }	
	 					}	 					
	 				}else{
	 					break;	 					
	 				}
	 			}	 	 			
 			} 			
 		}
 		pr($log);
	}
	
		
	
	

}
?>
